package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Notice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;


public class UserContextUtil {
    public static final String USER_IN_SESSION ="USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION ="EXPRESSION_IN_SESSION";
    public static final String EXCEPTIONS_IN_SESSION ="EXCEPTIONS_IN_SESSION";
    public static final String succeedCount_IN_SESSION ="succeedCount_IN_SESSION";
    public static final String errorCount_IN_SESSION ="errorCount_IN_SESSION";
    public static final String NOTICE_IN_SESSION ="NOTICE_IN_SESSION";

    //通过Spring MVC工具类直接获得session
    public static HttpSession getSession() {
        ServletRequestAttributes attrs =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }

    //把用户对象设置进域中
    public static void  setUserContext(Employee employee){
        getSession().setAttribute(USER_IN_SESSION,employee);
    }
    //把用户权限设置进域中
    public static void  setPermission(List<String> list){
        getSession().setAttribute(EXPRESSION_IN_SESSION,list);
    }

    //获取用户信息
    public static Employee getUserContext(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }
    //获取所有权限信息
    public static List<String> getPermission(){
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }

    //把导入异常设置进域中
    public static void  setExceptions(List<String> list){
        getSession().setAttribute(EXCEPTIONS_IN_SESSION,list);
    }

    //获取导入异常信息
    public static Employee getExceptions(){
        return (Employee) getSession().getAttribute(EXCEPTIONS_IN_SESSION);
    }
    //把成功计数设置进域中
    public static void  setSucceedCount(int SucceedCount){
        getSession().setAttribute(succeedCount_IN_SESSION,SucceedCount);
    }

    //获取成功导入计数信息
    public static Employee getSucceedCount(){
        return (Employee) getSession().getAttribute(succeedCount_IN_SESSION);
    }
    //把成功计数设置进域中
    public static void  setErrorCount(int SucceedCount){
        getSession().setAttribute(errorCount_IN_SESSION,SucceedCount);
    }

    //获取成功导入计数信息
    public static Employee getErrorCount(){
        return (Employee) getSession().getAttribute(errorCount_IN_SESSION);
    }

    //把未读公告设置进域中
    public static void  setNoticeContext(List<Notice> SucceedCount){
        getSession().setAttribute(NOTICE_IN_SESSION,SucceedCount);
    }

    //获取未读公告信息
    public static List<Notice> NoticeContext(){
        return (List<Notice>) getSession().getAttribute(NOTICE_IN_SESSION);
    }
}
